import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 登录框状态
    isOpenLogin: false,
    // toast变量
    toast: {
      show: false,
      content: '',
      icon: ''
    },
    // 购物车数量
    cartNum: 0
  },
  getters: {
  },
  mutations: {
    // 购物车数量加
    cartAdd(state) {
      state.cartNum++
    },
    cartSub(state) {
      if (state.cartNum > 0) {
        state.cartNum--
      }
    },
    // 修改登录框状态
    changeOpenLogin(state, payload) {
      state.isOpenLogin = payload
    },
    // 修改toast
    changeToast(state, payload) {
      state.toast.show = true,
        state.toast.content = payload.content
      switch (payload.icon) {
        // 警告
        case 'info': state.toast.icon = 'icon-toast-jinggao'
          break;
        // 成功
        case 'success': state.toast.icon = 'icon-toast_chenggong'
          break;
        // 失败
        case 'danger': state.toast.icon = 'icon-toast-shibai_huaban'
          break;
        // 加载中
        case 'loding': state.toast.icon = 'icon-loading'
          break;
        default: state.toast.icon = 'icon-toast-jinggao'
          break
      }
    },
    // 关闭toast
    closetoast(state) {
      state.toast.show = false
    }
  },
  actions: {
    // 一段时间内toast消失
    asyncclosetoast({ commit }, payload) {
      commit('changeToast', payload);
      setTimeout(() => {
        commit('closetoast')
      }, 2000)
    }
  },
  modules: {
  }
})
